package com.example.mvpdemo.login.view;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.example.mvpdemo.R;
import com.example.mvpdemo.base.BaseActivity;
import com.example.mvpdemo.login.contract.LoginContract;
import com.example.mvpdemo.login.presenter.LoginPresenter;
import com.example.mvpdemo.utils.ToastUtil;

/**
 * name 登录
 * desc
 * author Sy
 * email 564379992@qq.com
 * created 2020/5/6 16:11
 */
public class LoginActivity extends BaseActivity<LoginPresenter, LoginContract.VP> {

    private ImageButton ib_close;
    private EditText et_phone;
    private EditText et_pass;
    private Button btn_login;

    @Override
    public LoginPresenter getmPresenterInstance() {
        return new LoginPresenter();
    }

    @Override
    public int setLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        ib_close  = findViewById(R.id.ib_close);
        et_phone  = findViewById(R.id.et_phone);
        et_pass   = findViewById(R.id.et_pass);
        btn_login = findViewById(R.id.btn_login);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        btn_login.setOnClickListener(this);
    }

    @Override
    public void destory() {

    }

    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.btn_login:
               String phone = et_phone.getText().toString().trim();
               String pass  = et_pass.getText().toString().trim();
               getContract().requestLogin(phone, pass);
               break;
       }
    }

    @Override
    public LoginContract.VP getContract() {
        return new LoginContract.VP() {
            @Override
            public void requestLogin(String phone, String pass) {
                if(phone.isEmpty()){
                    ToastUtil.show(LoginActivity.this,"请输入手机号");
                    return;
                }
                if(pass.isEmpty()){
                    ToastUtil.show(LoginActivity.this,"请输入密码");
                    return;
                }
                mPresenter.getContract().requestLogin(phone,pass);
            }

            @Override
            public void responseLoginResult(boolean loginStatusResult) {
                if(loginStatusResult){
                    ToastUtil.show(LoginActivity.this,"登录成功");
                }else{
                    ToastUtil.show(LoginActivity.this,"登录失败");
                }
            }
        };
    }

}
